home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2896 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  98 lines

  1. Path: news.smartlink.net!usenet
  2. From: thomas@smartlink.net (tom)
  3. Newsgroups: comp.lang.c++
  4. Subject: help w/ operator overloading
  5. Date: 20 Jan 1996 07:48:02 GMT
  6. Organization: none
  7. Message-ID: <4dq6ni$nrq@frodo.smartlink.net>
  8. NNTP-Posting-Host: pm35.smartlink.net
  9. X-Newsreader: Cyberjack News 7.00
  10.  
  11. Help,
  12. Anyone know why I don't get into my XPoint + operator when adding the classes
  13. together? The Z works but not the X & Y.
  14.  
  15. Thanks
  16. ==============================================================================
  17.  
  18. #include <stdio.h>
  19.  
  20. class XPoint {
  21. private:
  22.     double x,y;
  23.  
  24. public:
  25.     double & X(){ return x;}
  26.     double & Y(){ return y;}
  27.  
  28.     XPoint(double _x, double _y):x(_x),y(_y){}
  29.     XPoint():x(0.0),y(0.0){}
  30.  
  31.     virtual XPoint operator+( XPoint &pt){
  32.         x = x + pt.X();
  33.         y = y + pt.Y();
  34.         return *this;
  35.     }
  36.  
  37.     virtual XPoint operator=( XPoint &pt){
  38.         x =  pt.X();
  39.         y =  pt.Y();
  40.         return *this;
  41.     }
  42.  
  43. };
  44.  
  45.  
  46. class XPoint3D: virtual public XPoint{
  47. private:
  48.     double z;
  49. public:
  50.     double &Z(){ return z;}
  51.     XPoint3D(double _x, double _y, double _z):XPoint(_x,_y),z(_z){}
  52.     XPoint3D():XPoint(),z(0.0){}
  53.  
  54.     XPoint3D operator+( XPoint3D &pt){
  55.         Z() = Z() + pt.Z();
  56.         return *this;
  57.     }
  58.     XPoint3D operator=( XPoint3D &pt){
  59.         Z() = pt.Z();
  60.         return *this;
  61.     }
  62. };
  63.  
  64.  
  65.  
  66. void main(){
  67.  
  68.     XPoint3D a;
  69.     XPoint3D b(10,10,10);
  70.     XPoint3D c(10,10,10);
  71.  
  72.     printf( "a= (%f,%f)\n",a.X(),a.Y() );
  73.     printf( "b= (%f,%f)\n",b.X(),b.Y() );
  74.     printf( "c= (%f,%f)\n",c.X(),c.Y() );
  75.  
  76.     a = (b + c);
  77.  
  78.     printf("\n\n");
  79.     printf( "a= (%f,%f)\n",a.X(),a.Y() );
  80.     printf( "b= (%f,%f)\n",b.X(),b.Y() );
  81.     printf( "c= (%f,%f)\n",c.X(),c.Y() );
  82.  
  83.     double x,y,z;
  84.     y=z=10.0;
  85.     x=y+z;
  86.     printf("\n\nx=%f,y=%f,z=%f\n",x,y,z); 
  87. }
  88.  
  89. Output:
  90. a=0,0
  91. b=10,10
  92. c=10,10
  93.  
  94. a=0,0
  95. b=10,10
  96. c=10,10
  97.  
  98.